﻿using AutoMapper;
using CarAppDomain.CarParts.Entities;
using CarAppDomain.CarParts.IRepository;
using CarAppDomain.CarParts.IService;
using CarAppDomain.CarType.Entities;
using CarAppDomain.CarType.IRepository;
using CarAppDto.CarParts.CarPartsTypeDtos;
using CarAppService.Sys.IServices;
using D.Util.UtilCache;
using D.UtilCore.Dtos;
using D.UtilCore.Utils;
using D.UtilSqlsugarOrm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace CarAppDomain.CarParts.Service
{
    public class VehicleSeriesCarPartsTypeService : BaseService<VehicleSeriesCarPartsTypeEntity>, IVehicleSeriesCarPartsTypeService
    {
        private readonly IMapper _mapper;
        private readonly ICacheService _cache;
        private readonly IVehicleSeriesCarPartsTypeEntityRepository _repository;

        /// <summary>
        /// 构造注入
        /// </summary>
        public VehicleSeriesCarPartsTypeService(IMapper mapper, ICacheService cache, IVehicleSeriesCarPartsTypeEntityRepository repository)
        {
            // 用于base方法
            _baseDal = repository;
            _repository = repository;
            _cache = cache;
            _mapper = mapper;
        }

        #region 增删改查


        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="reqModel"></param>
        /// <returns></returns>
        public async Task<ApiResult<int>> AddVehicleSeriesCarPartsTypeAsync(ReqAddVehicleSeriesCarPartsType reqModel)
        {
            var exists = await _repository.ExistsAsync(m => m.Name == reqModel.Name );
            if (exists)
            {
                return new ApiResult<int>() { Code = StatusCodeEnum.Waring, ErrorMsg = "名称重复" };
            }
            if (!string.IsNullOrWhiteSpace(reqModel.ParentId))
            {
                exists = await _repository.ExistsAsync(m =>m.Id== reqModel.ParentId);
                if (!exists)
                {
                    return new ApiResult<int>() { Code = StatusCodeEnum.Waring, ErrorMsg = "上级信息不存在" };
                }
            }
            var insertEntity = _mapper.Map<ReqAddVehicleSeriesCarPartsType, VehicleSeriesCarPartsTypeEntity>(reqModel);
            insertEntity.DeleteFlag = false;
            insertEntity.CreateTime = DateTime.Now;
            insertEntity.Id = Guid.NewGuid().ToString();
            var dbres = await _repository.AddAsync(insertEntity);
            return dbres.NormalResponse();
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task<ApiResult<int>> DeleteVehicleSeriesCarPartsTypeAsync(List<string> ids)
        {
            var dbres = await _repository.DeleteAsync(m => ids.Contains(m.Id));
            return dbres.NormalResponse();
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="reqModel"></param>
        /// <returns></returns>
        public async Task<ApiResult<int>> UpdateVehicleSeriesCarPartsTypeAsync(ReqUpdateVehicleSeriesCarPartsType reqModel)
        {

            var updateEntity = await _repository.GetModelAsync(m => m.Id == reqModel.Id);
            if (updateEntity == null)
            {
                return new ApiResult<int>() { Code = StatusCodeEnum.Waring, ErrorMsg = "该信息不存在" };
            }
            var exists = await _repository.ExistsAsync(m => m.Id != reqModel.Id && (m.Name == reqModel.Name));
            if (exists)
            {
                return new ApiResult<int>() { Code = StatusCodeEnum.Waring, ErrorMsg = "名称重复" };
            }
            if (!string.IsNullOrWhiteSpace(reqModel.ParentId))
            {
                exists = await _repository.ExistsAsync(m => m.Id == reqModel.ParentId);
                if (!exists)
                {
                    return new ApiResult<int>() { Code = StatusCodeEnum.Waring, ErrorMsg = "上级信息不存在" };
                }
            }
            updateEntity.Name = reqModel.Name;
            updateEntity.EnglishName = reqModel.EnglishName;
            updateEntity.UpdateTime = DateTime.Now;
            updateEntity.UpdateUser = reqModel.UpdateUser;
            updateEntity.TypeCode=reqModel.TypeCode;
            updateEntity.CarPartsTypeId=reqModel.CarPartsTypeId;
            updateEntity.ParentId = reqModel.ParentId;
            var dbres = await _repository.UpdateAsync(updateEntity);
            return dbres.NormalResponse();
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="reqModel"></param>
        /// <returns></returns>
        public async Task<ApiResult<List<RepVehicleSeriesCarPartsType>>> QueryVehicleSeriesCarPartsTypeListAsync(ReqQueryVehicleSeriesCarPartsTypeList reqModel)
        {
            Expression<Func<VehicleSeriesCarPartsTypeEntity, bool>> where = null;
            if (!string.IsNullOrEmpty(reqModel.Name))
            {
                where = where.And(m => m.Name.Contains(reqModel.Name));
            }

            if (!string.IsNullOrEmpty(reqModel.EnglishName))
            {
                where = where.And(m => m.EnglishName.Contains(reqModel.EnglishName));
            }
            if (!string.IsNullOrEmpty(reqModel.VehicleSeriesId))
            {
                where = where.And(m => m.VehicleSeriesId==reqModel.VehicleSeriesId);
            }

            var dbres = await _repository.GetListAsync(where);
            var res = _mapper.Map<List<VehicleSeriesCarPartsTypeEntity>, List<RepVehicleSeriesCarPartsType>>(dbres);
            return res.GetResponse();
        }

        /// <summary>
        /// 获取用户分页
        /// </summary>
        /// <param name="reqQueryAdminPage"></param>
        /// <returns></returns>
        public async Task<ApiResult<PageModel<RepVehicleSeriesCarPartsType>>> QueryVehicleSeriesCarPartsTypePagesAsync(ReqQueryVehicleSeriesCarPartsTypePage reqQueryPage)
        {
            Expression<Func<VehicleSeriesCarPartsTypeEntity, bool>> where = null;
            if (!string.IsNullOrEmpty(reqQueryPage.Name))
            {
                where = where.And(m => m.Name.Contains(reqQueryPage.Name));
            }

            if (!string.IsNullOrEmpty(reqQueryPage.EnglishName))
            {
                where = where.And(m => m.EnglishName.Contains(reqQueryPage.EnglishName));
            }
            if (!string.IsNullOrEmpty(reqQueryPage.VehicleSeriesId))
            {
                where = where.And(m => m.VehicleSeriesId == reqQueryPage.VehicleSeriesId);
            }

            var dbres = await _repository.GetPagesAsync(reqQueryPage.PageSize, reqQueryPage.PageNo, where, m => m.CreateTime);
            var res = new PageModel<RepVehicleSeriesCarPartsType>()
            {
                PageNo = dbres.PageNo,
                PageSize = dbres.PageSize,
                TotalCount = dbres.TotalCount,
                TotalPage = dbres.TotalPage,
                Data = _mapper.Map<List<VehicleSeriesCarPartsTypeEntity>, List<RepVehicleSeriesCarPartsType>>(dbres.Data)
            };
            return res.GetResponse();
        }

        /// <summary>
        /// 查询单个
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<ApiResult<RepVehicleSeriesCarPartsType>> GetVehicleSeriesCarPartsTypeAsync(string id)
        {
            var dbres = await _repository.GetModelAsync(m => m.Id == id);
            var res = _mapper.Map<VehicleSeriesCarPartsTypeEntity, RepVehicleSeriesCarPartsType>(dbres);
            return res.GetResponse();
        }

        #endregion
    }
}
